<?php

namespace app\api\controller\web;

use app\admin\model\Linedevice;
use app\admin\model\Org;
use app\admin\model\Role;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdaytrade;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproombook;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\api\service\BalanceService;
use app\api\service\DeviceService;
use app\api\service\MtService;
use app\api\service\MtUtil;
use app\api\service\RoomService;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function indexSum(){

        $user = $this->auth->getUser();
        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $shopModel = new Shop();
        $shopCount = $shopModel->where('org_id',$org['id'])->count('id');

        $shopOrderModel = new Shoporder();
        $today = $shopOrderModel->whereTime('create_time','today')->where('org_id',$org['id'])->where('status','not in',['paying','refund'])->sum('amount');
        $yes = $shopOrderModel->whereTime('create_time','yesterday')->where('org_id',$org['id'])->where('status','not in',['paying','refund'])->sum('amount');

        $ret = array();
        $ret['shopCount'] = $shopCount;
        $ret['todayAmount'] = $today;
        $ret['yestAmount'] = $yes;

        $this->success('',$ret);
    }

    public function shopData(){


        $startDate = $this->request->param('startDate');
        $endDate = $this->request->param('endDate');

        $startTime = strtotime($startDate." 00:00:00");
        $endTime = strtotime($endDate." 23:59:59");

        $user = $this->auth->getUser();
        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();


        $shopTradeDayModel = new Shopdaytrade();

        $sql = "select sum(amount) as amount ,shop_id,s.name from q_shop_order t  left join q_shop s on t.shop_id = s.id where t.org_id = ".$org['id']." and t.create_time >= $startTime and t.create_time <= $endTime and t.status not in ('paying','refund') group by shop_id order by amount desc";
        $list = $shopTradeDayModel->query($sql);
        $this->success('',$list);
    }

    public function tradeDay(){

        $startDate = $this->request->param('startDate');
        $endDate = $this->request->param('endDate');
        $shopTradeDayModel = new Shopdaytrade();
        $newList = array();


        $user = $this->auth->getUser();
        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();
        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $today = date("Y-m-d");
        $h = date('H');
        if(strtotime($startDate) - strtotime($endDate) == 0){

            if($today == $startDate){

                for($i = 0;$i<=$h;$i++){

                    if($i <10){
                        $i = "0".$i;
                        if($i == 9){
                            $end = ($i+1);

                        }else{
                            $end = "0".($i+1);

                        }
                    }else{
                        if($i == 23){
                            $end = $i;
                        }else{
                            $end = $i +1;

                        }
                    }

                    $start  = $startDate." ".($i).":00";
                    $end  = $startDate." ".($end).":59";
                    $startTime = strtotime($start);
                    $endTime = strtotime($end);
                    $sql = "select sum(amount) as amount,count(id) as total  from q_shop_order where org_id = ".$org['id']." and create_time >= $startTime and create_time <= $endTime and status not in ('paying','refund')";
                    $list = $shopTradeDayModel->query($sql);
                    foreach ($list as $key=>$val){
                        $val['time'] = $i.":00";
                        if($val['amount'] > 0){
                            $newList[] = $val;
                        }
                    }
                }
                $this->success('',$newList);

            }else{

                for($i = 0;$i<=23;$i++){

                    if($i <10){
                        $i = "0".$i;
                        if($i == 9){
                            $end = ($i+1);

                        }else{
                            $end = "0".($i+1);

                        }
                    }else{
                        if($i == 23){
                            $end = $i;
                        }else{
                            $end = $i +1;

                        }
                    }

                    $start  = $startDate." ".($i).":00";
                    $end  = $startDate." ".($end).":59";
                    $startTime = strtotime($start);
                    $endTime = strtotime($end);
                    $sql = "select sum(amount) as amount,count(id) as total  from q_shop_order where org_id = ".$org['id']." and create_time >= $startTime and create_time <= $endTime and status not in ('paying','refund')";
                    $list = $shopTradeDayModel->query($sql);
                    foreach ($list as $key=>$val){
                        $val['time'] = $i.":00";
                        if($val['amount'] > 0){
                            $newList[] = $val;
                        }
                    }
                }
                $this->success('',$newList);

            }
        }else{


            $day = diff_date($startDate,$endDate);
            $dates = array();
            $dates[] = $startDate;
            $d = 2;
            $isMode = -1;


            for($i = 1;$i<=$day;$i++){
                $dates[] = date("Y-m-d", strtotime("+".$i." day",strtotime($startDate)));
            }

            if(count($dates) >10){
                $isMode= 1;
            }
            if(count($dates) >30){
                $this->error("跨度查询只支持1个月");
            }

            foreach ($dates as $key=>$val){
                $startTime = strtotime($val." 00:00:00");
                $endTime = strtotime($val." 23:59:59");
                $sql = "select sum(amount) as amount,count(id) as total  from q_shop_order where org_id = ".$org['id']." and create_time >= $startTime and create_time <= $endTime and status not in ('paying','refund')";
                $list = $shopTradeDayModel->query($sql);


                foreach ($list as $k=>$v){

                    //error_log($key."===ddd===".$key%2);
                    if($isMode == 1){

                        if($key%2==0){
                            $v['time'] = "";
                        }else{
                            $v['time'] = $val;
                        }
                    }else{
                        $v['time'] = $val;
                    }
                    if($v['amount'] > 0 && $v['time'] != ""){
                        $newList[] = $v;
                    }
                }

            }

            $this->success('',$newList);

        }

    }




}
